package com.rp.demo.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.rp.demo.commons.ServerResponse;
import com.rp.demo.dto.Mail;
import com.rp.demo.dto.MessageEntity;
import com.rp.demo.laqu.LaQuConsumer;
import com.rp.demo.service.TestService;
import com.rp.demo.service.impl.LaquServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class MailController {

    @Autowired
    private TestService testService;

    @Autowired
    private LaquServiceImpl laquService;


    @Autowired
    private LaQuConsumer laQuConsumer;

    /**
     * 消息一致性
     * @param mail
     * @return
     */
    @PostMapping("/send")
    public ServerResponse sendMsg(@RequestBody Mail mail) {
        ServerResponse result = testService.send(mail);
        return result;
    }


    /**
     * 发送消息到拉取队列
     * @return
     */
    @GetMapping("/laquSend")
    public ServerResponse laquSend() {
        for (int i = 0; i < 32; i++) {
            MessageEntity entity = new MessageEntity();
            entity.setMsgId(IdUtil.fastUUID());
            entity.setMessage("这是第" + i + "条记录");
            log.info("----->{}", entity.getMessage());
            ServerResponse serverResponse = laquService.sendLaqu(entity);
            log.info("{}======={}======={}", i, JSONUtil.toJsonStr(entity), JSONUtil.toJsonStr(serverResponse));
        }

        return ServerResponse.success();
    }


    /**
     * 通过接口拉取队列中的消息进行消费
     * @return
     * @throws Exception
     */
    @GetMapping("/laquConsumer")
    public ServerResponse laquConsumer() throws Exception {
        laQuConsumer.consumerLaQu();

        return ServerResponse.success();
    }

}
